﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Win32;



namespace Grizzy_X
{
    public partial class MainForm : Form
    {
        public int CountForm = 0;
        public Childform f1;
        public Childform f2;
        public static string mapname;
        public static string layername;
        public static bool check=false;
        public TreeNode ParentNode;
        public TreeNode ChildNode;
        public MainForm()
        {
            InitializeComponent();
            ParentNode = new TreeNode("地图名称");
            LayerTree.Nodes.Add(ParentNode);
            ParentNode.Checked = true;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            StartForm startform = new StartForm();
            startform.ShowDialog();
            LineRst.SelectedIndex =0;
            GraphRst.SelectedIndex = 0;
            
        }

        private void New_Click(object sender, EventArgs e)
        {
            Controller controller = new Controller();
            controller.newProject();
        }

        private void Save_Click(object sender, EventArgs e)
        {

        }

        private void Layer_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //LayerTree.ContextMenuStrip = null;
        }

        private void Newmap_Click(object sender, EventArgs e)
        {
            NewMapForm newmapform = new NewMapForm();
            newmapform.ShowDialog();
            if (check == true)
            {
                panelmdi.Visible = true;
                if (CountForm == 0)
                {
                    ParentNode.Text = mapname;
                    f1 = new Childform();
                    CountForm++;
                    f1.FormBorderStyle = FormBorderStyle.None;
                    f1.Height = 464;
                    f1.Width = 758;
                    f1.MdiParent = this;
                    f1.Parent = this.panelmdi;
                    f1.Text = "子窗体" + CountForm;
                    if (this.WindowState == FormWindowState.Maximized)
                    {
                        f1.Width = 1080;
                        f1.Height = LayerTree.Height;
                    }
                    f1.Show();
                   
                }
                check = false;
            }
        }
        private void ChildForm_Move(object sender, EventArgs e)
        {
            if (f2.Location.Y <0)
                f2.Location= new Point(f2.Location.X,0);
        }
        private void Colorize_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
        }

        private void Font_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowDialog();
        }

        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                logo.Visible = true;
            }
            if (CountForm != 0)
            {
                if (this.WindowState == FormWindowState.Maximized)
                {
                    f1.Width = 1080;
                    f1.Height = LayerTree.Height;
                }
                if (this.WindowState == FormWindowState.Normal)
                {
                    f1.Width = 758;
                    f1.Height = 464;
                }
            }
            else
            {//Save
            }
        }

        private void ShowMDI_Click(object sender, EventArgs e)
        {
            if(CountForm!=0)
            {
                f2 = new Childform();
                CountForm++;
                f2.MdiParent = this;
                f2.Parent = this.panelmdi;
                f2.Text = "子窗体" + CountForm;
                f2.BringToFront();
                f2.Show();
                f2.Move+=new EventHandler(ChildForm_Move);
            }
        }

        private void Layer_MouseClick(object sender, MouseEventArgs e)
        {
            LayerTree.ContextMenuStrip = null;
            if (e.Button == MouseButtons.Right)
            {
                TreeNode selectNode = LayerTree.GetNodeAt(e.X, e.Y);
                if (selectNode.Level == 1)
                {
                    LayerTree.ContextMenuStrip = LayerMenu;
                    LayerTree.SelectedNode = selectNode;
                }
                if (selectNode.Level == 0)
                {
                    LayerTree.ContextMenuStrip = MapMenu;
                    LayerTree.SelectedNode = selectNode;
                }
            }
        }

        private void Import_Click(object sender, EventArgs e)
        {
            ImportForm importform = new ImportForm();
            importform.ShowDialog();
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void VisitWeb_Click(object sender, EventArgs e)
        {
            Process.Start("www.sina.com");

        }

        private void NewLayer_Click(object sender, EventArgs e)
        {
            NewLayerForm newlayerform=new NewLayerForm();
            newlayerform.ShowDialog();
            if (check)
            {
                ChildNode = new TreeNode(layername);
                ChildNode.ImageIndex = 1;
                ChildNode.SelectedImageIndex = 1;
                ParentNode.Nodes.Add(ChildNode);
                ChildNode.Checked = true;
                LayerTree.ExpandAll();
                check = false;
            }
        }

        private void LayerMenu_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            LayerTree.ContextMenuStrip = null;
        }

        private void MapMenu_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            LayerTree.ContextMenuStrip = null;
        }

        private void ChangeLegend_Click(object sender, EventArgs e)
        {
            LegendForm legendform = new LegendForm();
            legendform.ShowDialog();
        }

      
    }
}
